package com.fairytask.engine.components.button
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import mx.events.MoveEvent;
	
	
	/**
	 * 有特效的按钮按钮 
	 * @author xujh
	 * 
	 */
	public class FlashButton extends BaseComponent
	{
		/**按钮皮肤区域**/
		private var _btnSkinSpace:Sprite;
		
		/**按钮一般状态*/
		private var _buttonNormal:MovieClip;
		
		/**有特效状态*/
		private var _buttonActive:MovieClip;
		
		/**禁用状态*/
		private var _buttonUnallowed:MovieClip;
		
		/**按钮当前状态**/
		private var _currentState:String = ButtonState.NORMAL;
		
		/**是否活动**/
		private var _enabled:Boolean = true;
		
		/**
		 * 
		 * @param skin 皮肤
		 * 
		 */
		public function FlashButton(skin:String = null) 
		{
			this.buttonMode = true;
			//获得按钮状态mc
			if(skin != null){
				var mc:MovieClip = ResManager.newResClass(skin) as MovieClip;
				_buttonNormal = mc.getChildByName("normal") as MovieClip;
				_buttonActive = mc.getChildByName("active") as MovieClip;
				_buttonUnallowed = mc.getChildByName("unallow") as MovieClip;
				
				_buttonNormal.x	= _buttonActive.x	= _buttonUnallowed.x = 0;
				_buttonNormal.y	= _buttonActive.y	= _buttonUnallowed.y = 0;
				//不定义高宽，默认
//				_buttonNormal.width	= _buttonActive.width = _buttonUnallowed.width =	width;
//				_buttonNormal.height = _buttonActive.height = _buttonUnallowed.height =	height;
			}
			
			_btnSkinSpace = new Sprite();
			_btnSkinSpace.mouseChildren = _btnSkinSpace.mouseEnabled = false;
			this.addChild(_btnSkinSpace);
			updataBtnState();
		}
		
		/**
		 * 更新按钮状态显示皮肤
		 * 
		 */
		private function updataBtnState():void
		{
			clearAll(_btnSkinSpace);
			
			var stateSkin:MovieClip;
			switch(_currentState){
				case ButtonState.NORMAL:
					stateSkin = _buttonNormal;
					break;
				case ButtonState.ACTIVE:
					stateSkin = _buttonActive;
					break;
				case ButtonState.UNALLOWED:
					stateSkin = _buttonUnallowed;
					break;
			}
			_btnSkinSpace.addChild(stateSkin);
		}
		
		/**
		 * 清空
		 */
		private function clearAll(space:Sprite):void
		{
			if(space == null)return; 
			while(space.numChildren > 0){
				space.removeChildAt(0);	
			}
		}
		
		override public function set enabled(value:Boolean):void
		{
			_enabled = value;
			if(! _enabled){
				currentState = ButtonState.UNALLOWED;
				this.mouseEnabled = this.mouseChildren = false;
			}else{
				currentState = ButtonState.NORMAL;
				this.mouseEnabled = this.mouseChildren = true;
			}
		}
		
		/**按钮当前状态**/
		public function get currentState():String
		{
			return _currentState;
		}
		
		/**
		 * @private
		 */
		public function set currentState(value:String):void
		{
			_currentState = value;
			updataBtnState();
		}

		
	}
}